<?php
// +----------------------------------------------------------------------
// | 07FLYCRM [基于ThinkPHP5.0开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.07fly.xyz
// +----------------------------------------------------------------------
// | Professional because of focus  Persevering because of happiness
// +----------------------------------------------------------------------
// | Author: 开发人生 <goodkfrs@qq.com>
// +----------------------------------------------------------------------


namespace app\ten\model;

/**
 * 菜单模型
 */
class TenMenu extends TenBase
{

    /**
     * 同步=>菜单栏目功能=》2步=》同步更新栏目数据，增加不存的栏目数据
     * @param array $data
     * @param int $pid
     * @return bool
     * Author: 开发人生 goodkfrs@qq.com
     * Date: 2021/8/5 0005 18:44
     */
    public function tenMenuImport($data = [], $pid = 0)
    {
        if (empty($data)) {
            return true;
        }
        foreach ($data as $v) {
            $map['url'] = ['=', $v['url']];
            $map['module'] = ['=', $v['module']];
            $info = $this->modelTenMenu->getInfo($map, true);

            //整理是否有下级
            $childs = '';
            if (isset($v['nodes'])) {
                $childs = $v['nodes'];
                unset($v['nodes']);
            }
            //当栏目不存在、添加栏目
            if (empty($info)) {
                if (!isset($v['pid'])) {
                    $v['pid'] = $pid;
                }
                $result = $this->modelTenMenu->setInfo($v);
            } else {//存在跳过
                $result = $info['id'];//设置本为上级栏目
                $this->tenMenuImport($childs, $result);
            }

            if (!$result) {
                return false;
            }
            if (!empty($childs)) {
                $this->tenMenuImport($childs, $result);
            }
        }
        return true;
    }


    /**
     * 获得所有指定id所有父级
     * @param int $deptid
     * @param array $data
     * @return array
     */
    public function getTenMenuAllPid($deptid = 0, $data = [])
    {
        $where['id'] = ['=', $deptid];
        $info = $this->modelTenMenu->getInfo($where, 'id,pid');
        if (!empty($info) && $info['pid']) {
            $data[] = $info['pid'];
            return $this->getTenMenuAllPid($info['pid'], $data);
        }
        return $data;
    }

}
